import { request } from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    hotActivities: [],
    hotRanking: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  toUrl(item) {
    if (
      item.currentTarget.dataset.item.urlPath &&
      item.currentTarget.dataset.item.urlPath
        .length > 0
    ) {
      wx.navigateTo({
        url:
          '/' +
          item.currentTarget.dataset.item.urlPath
      })
    }
  },

  getBanners() {
    request({
      url: '/mini/api/selectTeam/banners',
      method: 'POST',
      data: {
        type: '选队伍'
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          bannerList: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getHotRanking() {
    request({
      url: '/mini/api/selectTeam/hotGroup'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          hotRanking: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  createEvent() {
    wx.navigateTo({
      url: '/packagePlan/createEvent/createEvent'
    })
  },

  createRanks() {
    wx.navigateTo({
      url:
        '/packageSquare/createRanks/createRanks'
    })
  },

  signNow(item) {
    const app = getApp()
    request({
      url: '/mini/api/selectTeam/joinGroup',
      method: 'POST',
      data: {
        groupId: Number(
          item.currentTarget.dataset.item.id
        ),
        userId: Number(
          app.globalData.userInfo.userId
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.navigateTo({
          url:
            '/packageMy/chat/chat?conversationID=' +
            'GROUP' +
            item.currentTarget.dataset.item.id
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toActivity(item) {
    wx.navigateTo({
      url:
        '/packagePlan/activityDetail/activityDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  getIsShowTeam() {
    request({
      url: '/api/cfg/config/queryByType/MINI'
    }).then((resdata) => {
      if (resdata.code == 200) {
        if (
          resdata.data[0].value == true ||
          resdata.data[0].value == 'true'
        ) {
          this.getHotRanking()
        }
      } else {
        wx.showToast({
          title: resdata.message
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBanners()
    this.getIsShowTeam()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
